package com.bt.lms.dao.servise.impl;

import com.bt.lms.bo.PageConditionQueryApplyStudent;
import com.bt.lms.constant.BOConstant;
import com.bt.lms.entity.EnrollmentPlan;
import com.bt.lms.entity.RegStuInfo;
import com.bt.lms.entity.RegStuMaterial;
import com.bt.lms.dao.servise.RegStuDao;
import com.bt.lms.entity.page.Page;
import com.bt.lms.utils.JDBCUtils;

import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 报名学员dao实现
 *
 * @author 朱传博
 */
public class RegStuDaoImpl implements RegStuDao {
    @Override
    public int insert(String sql, RegStuInfo regStuInfo) throws ClassNotFoundException, SQLException {

        String registrationCode = regStuInfo.getRegistrationCode();
        String name = regStuInfo.getName();//姓名
        Integer gender = regStuInfo.getGender();//性别
        LocalDate birthday = regStuInfo.getBirthday();//出生日期
        Integer credType = regStuInfo.getCredType();//证件类型
        String credNo = regStuInfo.getCredNo();//证件号码
        String email = regStuInfo.getEmail();//邮箱
        String mobilePhone = regStuInfo.getMobilePhone();//移动电话
        Integer regWay = regStuInfo.getReqWay();
        String qq = regStuInfo.getQq();//qq
        String wechat = regStuInfo.getWechat();//微信


        //jdbc
        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql);


        //预编译
        prepareStatement.setString(1,registrationCode);
        prepareStatement.setString(2, name);
        prepareStatement.setInt(3, gender);
        prepareStatement.setDate(4, Date.valueOf(birthday));
        prepareStatement.setInt(5, credType);
        prepareStatement.setString(6, credNo);
        prepareStatement.setString(7, email);
        prepareStatement.setString(8, mobilePhone);
        prepareStatement.setInt(9, regWay);
        prepareStatement.setString(10, qq);
        prepareStatement.setString(11, wechat);


        //4.执行sql语句
        int i = prepareStatement.executeUpdate();


        return i;
    }


    public int insert1(String sql, RegStuInfo regStuInfo) throws SQLException {


        List<RegStuMaterial> regStuMaterials = regStuInfo.getRegStuMaterials();
        for (RegStuMaterial regStuMaterial : regStuMaterials) {


            Long id = regStuMaterial.getId();
            Integer stuRegMaterial = regStuMaterial.getStuRegMaterial();
            String url = regStuMaterial.getUrl();


            //JDBC
            Connection jdbc = JDBCUtils.JDBC();
            PreparedStatement prepareStatement = jdbc.prepareStatement(sql);


            prepareStatement.setLong(1, id);
            prepareStatement.setInt(2, stuRegMaterial);
            prepareStatement.setString(3, url);


            //执行sql语句
            int i = prepareStatement.executeUpdate();


        }


        return 0;
    }


    @Override
    public RegStuInfo selectStu(String sql, RegStuInfo regStuInfo, Long applyInfoId) throws SQLException {


        //JDBC
        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql);
        prepareStatement.setLong(1, applyInfoId);


        //执行sql语句
        ResultSet resultSet = prepareStatement.executeQuery(sql);
        boolean next = resultSet.next();
        if (next) {
            int id = resultSet.getInt("id");//id
            String req_code = resultSet.getString("req_code");//报名编号
            String name = resultSet.getString("name");//姓名
            int gender = resultSet.getInt("gender");//性别
            String birthday = resultSet.getString("birthday");//出生日期
            int cred_type = resultSet.getInt("cred_type");//证件类型
            String cred_no = resultSet.getString("cred_no");//证件号码
            String email = resultSet.getString("email");//邮件
            String mobile_phone = resultSet.getString("mobile_phone");//移动电话
            String qq = resultSet.getString("qq");//qq
            String wechat = resultSet.getString("wechat");//微信
            int req_way = resultSet.getInt("req_way");//报名方式
            long enp_plan_id = resultSet.getLong("enp_plan_id");//招生计划id


            //带走
            regStuInfo.setRegistrationCode(req_code);
            regStuInfo.setName(name);
            regStuInfo.setGender(gender);
            regStuInfo.setBirthday(LocalDate.parse(birthday));
            regStuInfo.setCredType(cred_type);
            regStuInfo.setCredNo(cred_no);
            regStuInfo.setEmail(email);
            regStuInfo.setMobilePhone(mobile_phone);
            regStuInfo.setQq(qq);
            regStuInfo.setWechat(wechat);
            regStuInfo.setReqWay(req_way);
            regStuInfo.setEnpID(enp_plan_id);


        }

        return regStuInfo;
    }

    public RegStuInfo selectStuMaterial(String sql1, RegStuInfo regStuInfo, Long applyInfoId) throws SQLException {


        //JDBC
        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql1);


        prepareStatement.setLong(1, applyInfoId);


        ResultSet typeRS = prepareStatement.executeQuery(sql1);
        List<Integer> types = new ArrayList<>();
        while (typeRS.next()) {
            int type = typeRS.getInt("type");
            types.add(type);

        }
        //带走
        List<RegStuMaterial> regStuMaterials = new ArrayList<>();
        regStuInfo.setRegStuMaterials(regStuMaterials);


        return regStuInfo;


    }


    @Override
    public EnrollmentPlan selectEnp(String sql3, Long enpID, Long applyInfoId) throws SQLException {


        //JDBC
        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql3);


        prepareStatement.setLong(1, applyInfoId);

        ResultSet resultSet1 = prepareStatement.executeQuery(sql3);


        boolean next1 = resultSet1.next();
        if (next1) {
            String name1 = resultSet1.getString("name");

            RegStuInfo regStuInfo = new RegStuInfo();
            //带走
            EnrollmentPlan enrollmentPlan = new EnrollmentPlan();//招生计划
            enrollmentPlan.setPlanName(name1);
            regStuInfo.setEnrollmentPlan(enrollmentPlan);

        }

        //关资源
        prepareStatement.close();

        return null;
    }


    public boolean editRegStuInfo(String sql, Long applyInfoID, RegStuInfo regStuInfo) throws SQLException {


        String name = regStuInfo.getName();//姓名
        Integer gender = regStuInfo.getGender();//性别
        LocalDate birthday = regStuInfo.getBirthday();//出生日期
        Integer credType = regStuInfo.getCredType();//证件类型
        String credNo = regStuInfo.getCredNo();//证件号码
        String email = regStuInfo.getEmail();//邮箱
        String mobilePhone = regStuInfo.getMobilePhone();//移动电话
        Integer reqWay = regStuInfo.getReqWay();//报名方式
        String qq = regStuInfo.getQq();//qq
        String wechat = regStuInfo.getWechat();//微信


        //JDBC
        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql);


        //预编译
        prepareStatement.setString(1, name);
        prepareStatement.setInt(2, gender);
        prepareStatement.setDate(3, Date.valueOf(birthday));
        prepareStatement.setInt(4, credType);
        prepareStatement.setString(5, credNo);
        prepareStatement.setString(6, email);
        prepareStatement.setString(7, mobilePhone);
        prepareStatement.setInt(8, reqWay);
        prepareStatement.setString(9, qq);
        prepareStatement.setString(10, wechat);
        prepareStatement.setLong(11, applyInfoID);

        //执行sql语句
        int i = prepareStatement.executeUpdate();


        return true;
    }


    public boolean deleteRegStuInfo(String sql, Long applyInfoID) throws SQLException {

        //JDBC
        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql);

        //预编译
        prepareStatement.setInt(1, BOConstant.DELETED_TRUE);
        prepareStatement.setLong(2, applyInfoID);


        //执行sql语句
        int i = prepareStatement.executeUpdate();


        return true;
    }

    @Override
    public Page<RegStuInfo> pageQuery(String sql, Page<PageConditionQueryApplyStudent> page) throws SQLException {

        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql);

        Integer currentPage = page.getCurrent();//当前页
        Integer sizePage = page.getSize();//每页多少条


        //预编译
        prepareStatement.setInt(1, currentPage);
        prepareStatement.setInt(2, sizePage);

        ResultSet resultSet = prepareStatement.executeQuery();

        Page<RegStuInfo> regStuInfoPage = new Page<>();


        List<RegStuInfo>regStuInfos = new ArrayList<>();
        regStuInfoPage.setRecords(regStuInfos);


        while (resultSet.next()) {

            String req_code = resultSet.getString("req_code");//报名编号
            String name = resultSet.getString("name");//姓名
            int gender = resultSet.getInt("gender");//性别
            int req_way = resultSet.getInt("req_way");//报名方式
            int req_material_status = resultSet.getInt("req_material_status");//报名材料状态
            int email_status = resultSet.getInt("email_status");//邮箱状态
            int account_status = resultSet.getInt("account_status");//帐号状态


            RegStuInfo regStuInfo = new RegStuInfo();
            regStuInfo.setRegistrationCode(req_code);
            regStuInfo.setName(name);
            regStuInfo.setGender(gender);
            regStuInfo.setReqWay(req_way);
            regStuInfo.setRegMaterialStatus(req_material_status);
            regStuInfo.setEmailStatus(email_status);
            regStuInfo.setAccountStatus(account_status);
            regStuInfos.add(regStuInfo);



        }


        return regStuInfoPage;
    }


}








